/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "hhdTScene.h"
#include "hhdGameTimer.h"

namespace hhd
{
    /**
    * The Game class will be the root class for a Game using the Hogshead Engine.  It will set up a
	* lot of stuff by default and also provides a virtual initialize method to setup any other 
	* Manager type objects which are made down the road.
    */

    typedef void (*PredrawCallback)(void);

    class IGame
    {
    public:
        IGame();
        virtual ~IGame();

        void initialize();
        void finalize();

        // returns pointer to the current scene
        SmartPtr<TScene> getCurrentScene() const;

        // run the game durrr
        void run();
		void pauseSystem();
		void unPauseSystem();
        void exit();

        //loads a scene and adds a reference to it in our game's _scene_dictionary, returns the key
        static const String loadScene(const String& in_filepath);

        //this will be used to unload a scene and it's content when we are done with it
        static bool unloadScene(const String& in_path);

        static void reloadScene(const String& in_path);

        static void getPreviousScenePath(String& in_path);

        //changes which scene is running
        static void changeScene(const String& in_filepath);

        //unloads current scene, loads new scene, and runs new scene
        static void transistScene(const String& in_filepath);

        // register a windows event callback function with the game
        void registerWindowsEventCallback(void* in_proc);

        void registerPredrawCallback(PredrawCallback in_callback)
        {
            _manager_predraw_callbacks.add(in_callback);
        }

		inline Vector<void*> const& getManagerCallbacks() const
		{
			return _manager_os_message_callbacks;
		}

        void setTargetFramerate(int in_rate)
        {
            _target_framerate = in_rate;
        }

    protected:
        /** User/Gameplay **/

        virtual void configure() = 0;
        virtual void initializeGameplayLibraries() = 0;
        virtual void finalizeGameplayLibraries() = 0;
        virtual void loadContent() = 0;

        // the current scene we update on each run
        SmartPtr<TScene> _current_scene;
        String _start_scene;

        bool _system_paused;

        uint32_t _target_framerate;

    private:
        void initializeEngineLibraries();
        void finalizeEngineLibraries();
        void initializeLibraries();
		void* getOsMessageConsumeFunction();

		void update(const GameTimer& in_timer);
		void updateOS();
		void updateGame();
		void updateTransitions();
		void draw();

		void safeTransistToScene(const String& in_filepath);
		void safeChangeToScene(const String& in_filepath);
		void safeReloadScene(const String& in_key);

        static IGame* _game;

		GameTimer _game_timer;
		String _new_scene_filepath;
        String _reload_scene_key;

        bool _reload_scene;

        bool _transist;
        bool _change;

        float _last_draw_time;
		bool _quit_signal;

        static String _previous_scene_path;

		Vector<void*> _manager_os_message_callbacks;
		Vector<PredrawCallback> _manager_predraw_callbacks;
    };
}
